package com.example.feidaolumanager.pages.ui.task

import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.feidaolumanager.api.RetrofitClient
import com.example.feidaolumanager.pojo.ResponseDTO
import com.example.feidaolumanager.pojo.Task
import com.example.feidaolumanager.pojo.User
import kotlinx.coroutines.launch

class TaskViewModel : ViewModel() {

    var tasks: MutableState<List<Task>> = mutableStateOf(listOf())
    val updateResponse = MutableLiveData<ResponseDTO<String>>()

    fun getTask(user: User) {

        viewModelScope.launch {

            val responseDTO = RetrofitClient.taskService.getTasks(
                account = user.account,
                level = user.level,
                ss_id = user.ss_id
            )

            tasks.value = responseDTO.data!!
            Log.i(TaskViewModel::class.toString(), "获取我的任务列表的长度为：${tasks.value.size}")
        }
    }

    fun updateTask(task: Task) {
        viewModelScope.launch {
            val responseDTO = RetrofitClient.taskService.updateTask(task)
            Log.i(TaskViewModel::class.toString(), "更新任务成功")
            updateResponse.value = responseDTO
        }
    }
}